﻿using ESB;
using Hxj.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ZYXCESB
{
    public class BMS
    {
        private static DbSession GetDbSession()
        {
            DbSession dbSessionDefault = new DbSession(DatabaseType.SqlServer, System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
            return dbSessionDefault;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="jf">积分</param>
        /// <param name="money"></param>
        /// <returns></returns>
        public static ESB.EsbJfMsg CheckJf(string id, string jf, string money)
        {
            decimal dmj = decimal.Parse(System.Configuration.ConfigurationManager.AppSettings["mj"]);//现金最多能用积分抵扣的最大积分数；
            decimal djm = decimal.Parse(System.Configuration.ConfigurationManager.AppSettings["jm"]);
            #region 返回信息
            ESB.EsbJfMsg esbmsg = new EsbJfMsg();
            #endregion
            Hxj.Model.yw_hyyeb hyyeb = null;
            DbSession dbSessionDefault = GetDbSession();
            Hxj.Model.yw_hyyeb yeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == id).ToFirst();
            if (yeb == null)
            {
                //throw new Exception(string.Format("EMPI：{0}不存在！", id));
                esbmsg.Status = "F";
                esbmsg.Msg = string.Format("EMPI：{0}不存在！", id);
                return esbmsg;
            }
            else
            {               
                if (yeb.FEMPI != null)
                {
                    Hxj.Model.yw_hyyeb tmpyeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == yeb.FEMPI).ToFirst();
                    if (tmpyeb == null)
                    {
                        esbmsg.Status = "F";
                        esbmsg.Msg = string.Format("EMPI：[{0}]的父EMPI:[{1}]不存在！", yeb.EMPI,yeb.FEMPI);
                        return esbmsg;
                        //throw new Exception(string.Format("FEepi不存在", yeb.FEMPI));
                    }
                    else
                    {
                        hyyeb = tmpyeb;
                    }                   
                }
                else
                {
                    hyyeb = yeb;
                }
                //=========
                //if((hyyeb.xyye==null?0:hyyeb.xyye) < decimal.Parse(money))
                //{
                //    esbmsg.Status = "F";
                //    esbmsg.Msg = string.Format("EMPI:{0},余额不足！", id);
                //    return esbmsg;
                //}
                if ((hyyeb.xyjf == null ? 0 : hyyeb.xyjf) < decimal.Parse(jf))
                {
                    esbmsg.Status = "F";
                    esbmsg.Msg = string.Format("EMPI:{0},积分不足！", id);
                    return esbmsg;
                }
                else
                {
                    if (decimal.Parse(jf) > decimal.Parse(money) * dmj * djm)
                    {
                        esbmsg.Status = "F";
                        esbmsg.Msg = string.Format("EMPI:{0},现有积分：{1},消费金额：{2}，最多只能抵扣积分：{3}", id, hyyeb.xyjf == null ? 0 : hyyeb.xyjf, decimal.Parse(money), decimal.Parse(money) * dmj * djm);
                        return esbmsg;
                    }
                }
                esbmsg.Status="T";
                esbmsg.Id = hyyeb.EMPI;
                esbmsg.Jf = jf;
                esbmsg.Money = (decimal.Parse(money) - JfToMoney(decimal.Parse(jf))).ToString();
                //=========
                return esbmsg;
                //=========
            }
        }

        public static decimal JfToMoney(decimal jf)
        {
            return jf / decimal.Parse(System.Configuration.ConfigurationManager.AppSettings["jm"]);
        }
        public static string CheckJf(string id)
        {
            Hxj.Model.yw_hyyeb hyyeb = null;
            DbSession dbSessionDefault = GetDbSession();
            Hxj.Model.yw_hyyeb yeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == id).ToFirst();
            if (yeb == null)
            {
                throw new Exception(string.Format("EMPI：{0}不存在！", id));
            }
            else
            {
                if (yeb.FEMPI != null)
                {
                    Hxj.Model.yw_hyyeb tmpyeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == yeb.FEMPI).ToFirst();
                    if (tmpyeb == null)
                    {
                        throw new Exception(string.Format("FEepi不存在", yeb.FEMPI));
                    }
                    else
                    {
                        hyyeb = tmpyeb;
                    }
                }
                else
                {
                    hyyeb = yeb;
                }
                return hyyeb.xyjf.ToString();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">empi号</param>
        /// <returns></returns>
        public static bool HasFather(string id)
        {
            DbSession dbSessionDefault = GetDbSession();
            Hxj.Model.yw_hyyeb yeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == id).ToFirst();
            if (yeb == null)
            {
                throw new Exception(string.Format("EMPI：{0}不存在！", id));
            }
            else
            {
                if (yeb.FEMPI != null)
                {
                    Hxj.Model.yw_hyyeb tmpyeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == yeb.FEMPI).ToFirst();
                    if (tmpyeb == null)
                    {
                        throw new Exception(string.Format("FEepi不存在", yeb.FEMPI));
                    }
                    else
                    {
                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
        }
    
    }
}